package game;

import card.Cardset;
import player.Player;
import io.IOManager;
import game.Game;

/**
 * Game class controls the game's flow. Most game flow controlling logic could
 * be found here.
 * 
 * 
 * @version 2.0.0
 * 
 */
public class GameTurn {

	private Player turnPlayer;
	private TurnResult result;

	public GameTurn(Player player) {
		turnPlayer = player;
		result = TurnResult.PENDING;
	}

	/**
	 * Check if a play bigger than the play in current game turn
	 * 
	 * @return
	 */
	public boolean isBiggerPlay() {
		return (result == TurnResult.CONFIRMED);
	}

	/**
	 * return the selected cards
	 * 
	 * @param ioManager
	 * @return
	 */
	public Cardset getCardSelection(IOManager ioManager) {
		char option;
		Cardset play;
		do {
			play = turnPlayer.startTurn();
			option = ioManager.ioConfirmPlay(play);
		} while (option == 'r');
		return play;
	}

	/**
	 * return the result of current turn
	 * 
	 * @param ioManager
	 * @param game
	 * @return
	 */
	public TurnResult play(IOManager ioManager, Game game) {

		Cardset play;
		play = getCardSelection(ioManager);

		while (play.count() != 0 && !game.acceptPlay(play)) {
			ioManager.ioNotBiggerPlayMessage();
			play = getCardSelection(ioManager);
		}

		result = (play.count() == 0) ? TurnResult.SKIPPED
				: TurnResult.CONFIRMED;

		turnPlayer.endTurn();
		return result;
	}

	/**
	 * return the player of current turn
	 * 
	 * @return
	 */
	public Player getTurnPlayer() {
		return turnPlayer;
	}

}
